<?php
/*
 * @Author: 杨红兵
 * @Date: 2022-10-04 16:39:13
 * @Last Modified by: 杨红兵
 * @Last Modified time: 2022-10-05 10:35:35
 */

namespace app\libraries\utils\command;

use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Output\OutputInterface;
use think\console\Output;

class OutputStyle implements OutputInterface
{
    /**
     * 真实的输出类
     *
     * @var Output
     * @Author YangHB
     * @DateTime 2022-10-04 16:41:03
     *
     */
    protected Output $output;
    public function __construct(Output $output)
    {
        $this->output = $output;
    }

    /**
     * 输出日志
     *
     * @Author YangHB
     * @DateTime 2022-10-04 16:43:17
     *
     * @param mixed $messages
     * @param boolean $newline
     * @param integer $options
     * @return void
     */
    public function write($messages, bool $newline = false, int $options = 0)
    {
        $this->output->write(is_string($messages) ? $messages : json_encode($messages, JSON_UNESCAPED_UNICODE), $newline, $options);
    }

    public function writeln($messages, int $options = 0)
    {
        $this->output->writeln(is_string($messages) ? $messages : json_encode($messages, JSON_UNESCAPED_UNICODE), $options);
    }

    public function setVerbosity(int $level)
    {
        $this->output->setVerbosity($level);
    }

    public function getVerbosity()
    {
        return $this->output->getVerbosity();
    }

    public function isQuiet()
    {
        return $this->output->isQuiet();
    }

    public function isVerbose()
    {
        return $this->output->isVerbose();
    }

    public function isVeryVerbose()
    {
        return $this->output->isVeryVerbose();
    }

    public function isDebug()
    {
        return $this->output->isDebug();
    }

    protected $decorated = true;

    public function setDecorated(bool $decorated)
    {
        $this->output->setDecorated($decorated);
        $this->decorated = $decorated;
    }

    public function isDecorated()
    {
        return $this->decorated;
    }

    protected $formatter;

    public function setFormatter(OutputFormatterInterface $formatter)
    {
        $this->formatter = $formatter;
    }

    public function getFormatter()
    {
        if (!$this->formatter) {
            $this->formatter = new OutputFormatter();
        }
        return $this->formatter;
    }
}
